/**
 * Created by ajhin on 7/11/2015.
 */
package com.societyGames2.convert
{
import org.as3commons.asblocks.parser.antlr.LinkedListTree;
import org.as3commons.asblocks.parser.antlr.as3.AS3Parser;

/*
+++CLASS_DEF
++++MODIFIERS
+++++PUBLIC: public
++++IDENT: Preloader
++++EXTENDS
+++++TYPE
++++++IDENT: MovieClip
++++IMPLEMENTS
+++++TYPE
++++++IDENT: ICase
++++TYPE_BLOCK
*/
public class ClassDefTreeConverter implements ITreeConverter
{
  //ITreeToCSharp-->
  public function convert(tree:LinkedListTree):String
  {
    var baseTypeTexts:Vector.<String> = new Vector.<String>();
    var i:int = 0;
    var childTree:LinkedListTree;
    for (i = 0; tree.children != null && i < tree.children.length; i++)
    {
      childTree = LinkedListTree(tree.getChild(i));
      if (childTree.type == AS3Parser.EXTENDS || childTree.type == AS3Parser.IMPLEMENTS)
      {
        baseTypeTexts.push(TreeConverters.convert(childTree));
      }
    }

    var source:String = "";
    for (i = 0; tree.children != null && i < tree.children.length; i++)
    {
      childTree = LinkedListTree(tree.getChild(i));
      if (childTree.type == AS3Parser.IDENT)
      {
        source += " class " + TreeConverters.tokenTreeConverter.convert(childTree);
        if (baseTypeTexts.length > 0)
        {
          source += " : ";
          for (var baseTypeIndex:int = 0; baseTypeIndex < baseTypeTexts.length; baseTypeIndex++)
          {
            if (baseTypeIndex > 0)
            {
              source += ", ";
            }
            source += baseTypeTexts[baseTypeIndex];
          }
        }
      }
      else if (childTree.type != AS3Parser.EXTENDS && childTree.type != AS3Parser.IMPLEMENTS)
      {
        if (childTree.type == AS3Parser.TYPE_BLOCK)
        {
          source += "\n";
        }
        source += TreeConverters.convert(childTree);
      }
    }
    return source;
  }
  //<--ITreeToCSharp
}
}
